import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'

dayjs.extend(utc)

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
//utc 时间格式化
export function utcToDateTimeFormat(utcDate: string, format: string = DATE_TIME_FORMAT) {
     return dayjs.utc(utcDate).utcOffset(8).format(format)
}

//获取当前时间
export function getCurrentTime() {
     return dayjs().format(DATE_TIME_FORMAT)
}

export const formateDate = (time: any, type: string = 'yyy-mm-dd') => {
     // 使用dayjs 把世界标准时间转换为北京时间
     const y = time.getFullYear()
     let m = time.getMonth() + 1
     m = m < 10 ? '0' + m : m
     let d = time.getDate()
     d = d < 10 ? '0' + d : d
     let h = time.getHours()
     h = h < 10 ? '0' + h : h
     let minute = time.getMinutes()
     minute = minute < 10 ? '0' + minute : minute
     let second = time.getSeconds()
     second = second < 10 ? '0' + second : second
     if (type === 'yyy-mm-dd') {
          return y + '-' + m + '-' + d
     } else {
          return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
     }
}
// 时间格式修改
export const timesjxg = (onvaltime: string, num: number = 0) => {
     // 将输入的日期字符串转换为Date对象
     const inputDate = new Date(onvaltime)
     // 增加时间
     inputDate.setSeconds(inputDate.getSeconds() + num)
     // 将日期对象转换为指定格式的字符串  yyy-mm-dd hh:mm:ss
     return formateDate(inputDate)
}
export const formatDate = (timeString: any, type: string = 'yyyy-mm-dd hh:mm:ss') => {
     if (!timeString) return ''
     const date = new Date(timeString)
     const year = date.getFullYear()
     const month = String(date.getMonth() + 1).padStart(2, '0')
     const day = String(date.getDate()).padStart(2, '0')
     const hour = String(date.getHours()).padStart(2, '0')
     const minute = String(date.getMinutes()).padStart(2, '0')
     const second = String(date.getSeconds()).padStart(2, '0')
     if (type == 'yyyy-mm-dd hh:mm:ss') {
          return `${year}-${month}-${day} ${hour}:${minute}:${second}`
     } else if (type == 'yyyy-mm-dd hh:mm') {
          return `${year}-${month}-${day} ${hour}:${minute}`
     } else if (type == 'yyyy-mm-dd') {
          return `${year}-${month}-${day}`
     } else {
          return ''
     }
}
export const dateToString = (current: any) => {
     if (!(current instanceof Date)) {
          return current
     }
     const o = {
          y: current.getFullYear(), //月份
          m: current.getMonth() + 1, //月份
          d: current.getDate(), //日
          h: current.getHours(), //小时
          mi: current.getMinutes(), //分
          s: current.getSeconds(), //秒
          q: Math.floor((current.getMonth() + 3) / 3), //季度
          S: current.getMilliseconds(), //毫秒
     }
     console.log(o.y + '-' + o.m + '-' + o.d + ' ' + o.h + ':' + o.mi + ':' + o.s, '时间')
     if (o.m <= 9 && o.d <= 9) {
          return o.y + '-0' + o.m + '-0' + o.d + ' ' + o.h + ':' + o.mi + ':' + o.s
     } else if (o.d <= 9) {
          return o.y + '-' + o.m + '-0' + o.d + ' ' + o.h + ':' + o.mi + ':' + o.s
     } else if (o.m <= 9) {
          return o.y + '-0' + o.m + '-' + o.d + ' ' + o.h + ':' + o.mi + ':' + o.s
     }
     return o.y + '-' + o.m + '-' + o.d + ' ' + o.h + ':' + o.mi + ':' + o.s
}
// 历程数据格式化
export const parseMileage = (mileage: any) => {
     let res = ''
     const mile = Number(mileage)
     if (!Number.isNaN(mile)) {
          res = 'K' + Math.floor(mile / 1000) + '+' + (mile % 1000).toFixed(0).padStart(3, '0')
     }
     return res
}
//
